package com.itdct.onflow.system.dict.listener

import com.itdct.onflow.core.config.OnflowConfig
import com.itdct.onflow.core.extend.logError
import com.itdct.onflow.core.extend.logInfo
import com.itdct.onflow.system.dict.global.DictLock
import com.itdct.onflow.system.dict.global.DictMapGlobal
import com.itdct.onflow.system.dict.manager.SysDictManager
import org.springframework.context.ApplicationListener
import org.springframework.context.event.ContextRefreshedEvent
import org.springframework.stereotype.Component

/**
 * @author Zhouwx
 * @date 2025/9/3 16:13:09
 * @version 1.0
 * @description
 */
@Component
class SysDictListener(
    val sysDictManager: SysDictManager,
    val onflowConfig: OnflowConfig
) : ApplicationListener<ContextRefreshedEvent> {
    override fun onApplicationEvent(event: ContextRefreshedEvent) {
        val enable = onflowConfig.dict.enable
        DictMapGlobal.enable = enable

        if (!enable) {
            logInfo("字典功能已关闭")
            return
        }

        try {
            DictLock.lock.lock()
            sysDictManager.initDict()
        } catch (e: Exception) {
            logError(e)
        } finally {
            DictLock.lock.unlock()
        }
    }
}